/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.maven;

import ws.quokka.core.bootstrap_util.Assert;
import ws.quokka.core.repo_spi.RepoArtifactId;
import ws.quokka.core.util.Strings;
import ws.quokka.core.version.Version;


/**
 *
 */
public class IdMapping {
    //~ Instance fields ------------------------------------------------------------------------------------------------

    private MavenId mavenId;
    private RepoArtifactId id;

    //~ Constructors ---------------------------------------------------------------------------------------------------

    public IdMapping(String mavenId, String id) {
        this(MavenId.parse(mavenId), RepoArtifactId.parse(id));
    }

    public IdMapping(MavenId mavenId, RepoArtifactId id) {
        this.mavenId = mavenId;
        Assert.isTrue((mavenId.getGroupId() != null) && (mavenId.getArtifactId() != null),
            "At least the groupId and artifactId must be specified for a mapping: " + mavenId.toString());
        Assert.isTrue(mavenId.getType() == null,
            "Do not specify types in id mappings, use a global type mapping instead: " + mavenId.toString());
        Assert.isTrue(id.getGroup() != null, "At least the group must be specified for a mapping: "
            + id.toShortString());

        String[] tokens = Strings.split(id.getGroup(), ".");
        this.id = id.merge(new RepoArtifactId(null, tokens[tokens.length - 1], null, (Version)null));
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     * Returns the equivalent quokka id from a maven id, or null if the mapping does not match the id supplied
     */
    public RepoArtifactId toId(MavenId mavenId) {
        String version = (id.getVersion() != null) ? id.getVersion().toString() : toVersion(mavenId.getVersion());

        return new RepoArtifactId(id.getGroup(), id.getName(), null, version);
    }

    private String toVersion(String version) {
        if (version.toLowerCase().endsWith("-snapshot")) {
            version = version.substring(0, version.length() - 8) + "ss";
        }

        return version;
    }

    public MavenId getMavenId() {
        return mavenId;
    }

    public RepoArtifactId getId() {
        return id;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if ((o == null) || (getClass() != o.getClass())) {
            return false;
        }

        IdMapping idMapping = (IdMapping)o;

        if ((id != null) ? (!id.equals(idMapping.id)) : (idMapping.id != null)) {
            return false;
        }

        if ((mavenId != null) ? (!mavenId.equals(idMapping.mavenId)) : (idMapping.mavenId != null)) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result = ((mavenId != null) ? mavenId.hashCode() : 0);
        result = (31 * result) + ((id != null) ? id.hashCode() : 0);

        return result;
    }
}
